package com.atguigu.gmall.realtime.app.marketing.func;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.app.marketing.bean.GmallEvent;
import com.atguigu.gmall.realtime.app.marketing.constant.MarketingConst;
import com.atguigu.gmall.realtime.utils.RedisUtil;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.phoenix.util.DateUtil;
import redis.clients.jedis.Jedis;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static com.atguigu.gmall.realtime.app.marketing.constant.MarketingConst.*;

public class UserActionCounter extends RichMapFunction<GmallEvent,GmallEvent> {

    Jedis jedis;

    @Override
    public void open(Configuration parameters) throws Exception {
         jedis= RedisUtil.getJedis();
    }

    @Override
    public GmallEvent map(GmallEvent gmallEvent) throws Exception {
        // 统计到redis中   每个用户各个行为的计数
        //   type ? hash        key?  user_action:uid:101        field?  order  或则 pay 或者 page     value?  数值
        // 读api 取某个用户的所有行为  hgetall     写api  为某个用户的某个行为加值      过期时间 取第二天0点 减当前时间
        Date tomDate0 = getTomDate0();

        if(gmallEvent.getTopic().equals(TOPIC_START)){
           String uid = gmallEvent.getData().getJSONObject("common").getString("uid");
           String key= MarketingConst.USER_ACTION_PREFIX+uid;
            incrAction(key,ACTION_START,tomDate0);
       }else if(gmallEvent.getTopic().equals(TOPIC_PAGE)){
            String uid = gmallEvent.getData().getJSONObject("common").getString("uid");
            JSONObject pageJsonObj = gmallEvent.getData().getJSONObject("page");
            String key= MarketingConst.USER_ACTION_PREFIX+uid;
            incrAction(key,ACTION_PAGE,tomDate0);
            if(pageJsonObj.getString("page_id").equals("search")){
                incrAction(key,ACTION_SEARCH,tomDate0);
            } else if (pageJsonObj.getString("page_id").equals("trade")) {
                incrAction(key,ACTION_ORDER,tomDate0);
            } else if (pageJsonObj.getString("page_id").equals("payment")) {
                incrAction(key,ACTION_PAY,tomDate0);
            }

        } else if (gmallEvent.getTopic().equals(TOPIC_ACTION)) {
            String uid = gmallEvent.getData().getJSONObject("common").getString("uid");
            JSONObject actionJsonObj = gmallEvent.getData().getJSONObject("action");
            String key= MarketingConst.USER_ACTION_PREFIX+uid;
            if(actionJsonObj.getString("action_id").equals("get_coupon")){
                incrAction(key,ACTION_GET_COUPON,tomDate0);
            } else if (actionJsonObj.getString("action_id").equals("cart_add")) {

                incrAction(key,ACTION_CART,tomDate0);
            } else if (actionJsonObj.getString("action_id").equals("favor")) {
                incrAction(key,ACTION_FAVOR,tomDate0);

            }
        }

        return gmallEvent;
    }


    private Date getTomDate0(){
        Date tomDate = DateUtils.addDays(new Date(), 1);
        Date tomDate0 = DateUtils.truncate(tomDate, Calendar.DATE);
         return  tomDate0 ;
    }

    private void incrAction(String key,String action,Date tomDate0){
        jedis.hincrBy(key, action,1L);
        jedis.expireAt(key,tomDate0.getTime()/1000);
    }
}
